package com.flyqiu.form.api.tools;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.flyqiu.common.tools.FlyQiuCollectionTools;
import com.flyqiu.form.common.exceptions.FlyQiuFormException;
import com.flyqiu.form.model.entity.FlyQiuTableConfig;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FlyQiuTableOptionTools {
    /**
     * 获取列信息
     *
     * @param config 表单配置
     * @return 列信息配置
     */
    public static JSONArray getColumns(FlyQiuTableConfig config) {
        return getColumns(config.getFormOption());
    }


    /**
     * 获取列信息
     *
     * @param tableOption 表单配置
     * @return 列信息配置
     */
    public static JSONArray getColumns(JSONObject tableOption) {
        if (tableOption == null || tableOption.isEmpty()) {
            return new JSONArray();
        }
        if (!tableOption.containsKey("column")) {
            return new JSONArray();
        }
        JSONArray column = tableOption.getJSONArray("column");
        if (FlyQiuCollectionTools.isEmpty(column)) {
            return new JSONArray();
        }

        for (int i = 0; i < column.size(); i++) {
            if (column.get(i) instanceof JSONObject) {
                continue;
            }
            column.set(i, JSONObject.from(column.get(i)));
        }
        return column;
    }

    public static JSONArray getColumns(FlyQiuTableConfig config, boolean loadGroup) {
        return getColumns(config.getFormOption(), loadGroup);
    }

    public static JSONArray getColumns(JSONObject tableOption, boolean loadGroup) {
        JSONArray result = new JSONArray();
        JSONArray columns = getColumns(tableOption);
        result.addAll(columns);
        if (!loadGroup) {
            return result;
        }
        List<JSONObject> avueGroups = getTableAvueGroups(tableOption);
        for (JSONObject group : avueGroups) {
            result.addAll(getColumns(group));
        }
        return result;
    }


    public static List<JSONObject> getTableAvueGroups(FlyQiuTableConfig config) {
        return getTableAvueGroups(config.getFormOption());
    }

    public static List<JSONObject> getTableAvueGroups(JSONObject formOption) {

        if (FlyQiuCollectionTools.isEmpty(formOption)) {
            return new ArrayList<>();
        }
        JSONArray groups = formOption.getJSONArray("group");
        if (FlyQiuCollectionTools.isEmpty(groups)) {
            return new ArrayList<>();
        }
        return groups.toList(JSONObject.class);
    }

    /**
     * 获取字段索引
     *
     * @param formConfig 表单配置
     * @return 列信息索引表
     */
    public static Map<String, Integer> getTableOptionFieldIndex(FlyQiuTableConfig formConfig) {
        JSONArray column = getColumns(formConfig);
        return getTableOptionFieldIndex(column);
    }

    /**
     * 获取字段索引
     *
     * @param column 列信息
     * @return 列信息索引表
     */
    public static Map<String, Integer> getTableOptionFieldIndex(JSONArray column) {
        Map<String, Integer> colFields = new HashMap<>();
        for (int i = 0; i < column.size(); i++) {
            JSONObject col = column.getJSONObject(i);
            if (!col.containsKey("prop") || StringUtils.isEmpty(col.getString("prop"))) {
                throw new FlyQiuFormException("请设置第" + (i + 1) + "组件的字段名");
            }
            String prop = col.getString("prop");
            if (colFields.containsKey(prop)) {
                throw new FlyQiuFormException("第" + colFields.get(prop) + "组件字段名称与第" + (i + 1) + "字段名重复");
            }
            colFields.put(prop, (i + 1));
        }
        return colFields;
    }

    /**
     * 获取字段索引
     *
     * @param formConfig 数据表配置
     * @return 列信息表 &gt;表字段,字段配置&lt;
     */
    public static Map<String, JSONObject> getTableColumnMap(FlyQiuTableConfig formConfig) {
        JSONArray column = getColumns(formConfig);
        return getTableColumnMap(column);
    }

    /**
     * 获取字段索引
     *
     * @param tableOption 数据表配置
     * @return 列信息表 &gt;表字段,字段配置&lt;
     */
    public static Map<String, JSONObject> getTableColumnMap(JSONObject tableOption) {
        JSONArray column = getColumns(tableOption);
        return getTableColumnMap(column);
    }

    /**
     * 获取字段索引
     *
     * @param column 列信息
     * @return 列信息表 &gt;表字段,字段配置&lt;
     */
    public static Map<String, JSONObject> getTableColumnMap(JSONArray column) {
        Map<String, JSONObject> colFields = new HashMap<>();
        for (int i = 0; i < column.size(); i++) {
            JSONObject col = column.getJSONObject(i);
            if (!col.containsKey("prop") || StringUtils.isEmpty(col.getString("prop"))) {
                throw new FlyQiuFormException("请设置第" + (i + 1) + "组件的字段名");
            }
            String prop = col.getString("prop");
            if (colFields.containsKey(prop)) {
                throw new FlyQiuFormException("第" + colFields.get(prop) + "组件字段名称与第" + (i + 1) + "字段名重复");
            }
            colFields.put(prop, col);
        }
        return colFields;
    }


}
